#!/bin/bash

#下载epel源
yum install epel-release -y
#zlib 压缩库 / openssl http服务 / pcre 正则表达式 / gcc make 编译工具 / wget 下载工具
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ make wget -y

#创建专用用户
id Wangguang|| useradd wangguang -s /sbin/nologin

#创建压缩包存放目录
mkdir /wangguang -p
cd /wangguang
#下载压缩包
wget https://nginx.org/download/nginx-1.21.4.tar.gz

#解压压缩包
tar xf nginx-1.21.4.tar.gz
cd nginx-1.21.4

#配置编译选项 --prefix：安装目录 --user/--group：指定运行用户 --with-http_ssl_module：启用 HTTPS 支持 --with-http_v2_module：启用 HTTP/2 支持
./configure --prefix=/usr/local/nginx --user=wangguang  --group=wangguang --with-http_ssl_module --with-http_v2_module 

#如果编译错误则退出脚本
if (( $? != 0)); then
	exit
fi

#编译安装
make -j 2
make install

#设置环境变量
echo "PATH=$PATH:/usr/local/nginx/sbin" >>/etc/bashrc 
source /etc/bashrc

#关闭防火墙
service firewalld stop
systemctl disable firewalld

#关闭selinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config

#设置开机自启
chmod +x /etc/rc.d/rc.local
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local 

/usr/local/nginx/sbin/nginx


